<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <!-- <el-form-item label="换货人id" prop="assigneeId">
      <el-input v-model="dataForm.assigneeId" placeholder="换货人id"></el-input>
    </el-form-item>
    <el-form-item label="换货人名称" prop="assigneeName">
      <el-input v-model="dataForm.assigneeName" placeholder="换货人名称"></el-input>
    </el-form-item>
    <el-form-item label="联系电话" prop="phone">
      <el-input v-model="dataForm.phone" placeholder="联系电话"></el-input>
    </el-form-item> -->
      <el-form-item label="优先级" prop="priority">
        <el-input v-model="dataForm.priority" placeholder="优先级"></el-input>
      </el-form-item>
      <!-- <el-form-item label="状态" prop="status">
        <el-input v-model="dataForm.status" placeholder="状态"></el-input>
      </el-form-item>
      <el-form-item label="总金额" prop="amount">
        <el-input v-model="dataForm.amount" placeholder="总金额"></el-input>
      </el-form-item>
      <el-form-item label="创建日期" prop="createTime">
        <el-input
          v-model="dataForm.createTime"
          placeholder="创建日期"
        ></el-input>
      </el-form-item>
      <el-form-item label="更新日期" prop="updateTime">
        <el-input
          v-model="dataForm.updateTime"
          placeholder="更新日期"
        ></el-input>
      </el-form-item> -->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        assigneeId: "",
        assigneeName: "",
        phone: "",
        priority: "",
        status: 0,
        amount: "",
        createTime: "",
        updateTime: ""
      },
      dataRule: {
        assigneeId: [
          { required: true, message: "换货人id不能为空", trigger: "blur" }
        ],
        assigneeName: [
          { required: true, message: "换货人名称不能为空", trigger: "blur" }
        ],
        phone: [
          { required: true, message: "联系电话不能为空", trigger: "blur" }
        ],
        priority: [
          { required: true, message: "优先级不能为空", trigger: "blur" }
        ],
        status: [{ required: true, message: "状态不能为空", trigger: "blur" }],
        amount: [
          { required: true, message: "总金额不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建日期不能为空", trigger: "blur" }
        ],
        updateTime: [
          { required: true, message: "更新日期不能为空", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/ware/exchange/info/${this.dataForm.id}`),
            method: "get",
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.assigneeId = data.exchange.assigneeId;
              this.dataForm.assigneeName = data.exchange.assigneeName;
              this.dataForm.phone = data.exchange.phone;
              this.dataForm.priority = data.exchange.priority;
              this.dataForm.status = data.exchange.status;
              this.dataForm.amount = data.exchange.amount;
              this.dataForm.createTime = data.exchange.createTime;
              this.dataForm.updateTime = data.exchange.updateTime;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/ware/exchange/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              assigneeId: this.dataForm.assigneeId,
              assigneeName: this.dataForm.assigneeName,
              phone: this.dataForm.phone,
              priority: this.dataForm.priority,
              status: this.dataForm.status,
              amount: this.dataForm.amount,
              createTime: this.dataForm.createTime,
              updateTime: this.dataForm.updateTime
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    }
  }
};
</script>
